package com.rabbit.demo.conf;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {

    @Bean
    public Queue queue(){
        Map<String,Object> props = new HashMap<>();
        props.put("x-message-ttl",10 * 1000);
        props.put("x-dead-letter-exchange","ex.go.dlx");
        props.put("x-dead-letter-routing-key","go.dlx");

        Queue queue = new Queue("q.go",true,false,false,props);
        return queue;
    }

    @Bean
    public Queue queueDlx(){
        Queue queue = new Queue("q.go.dlx",true,false,false);
        return queue;
    }

    @Bean
    public Exchange exchange(){
        DirectExchange exchange = new DirectExchange("ex.go",true,false,null);
        return exchange;
    }

    @Bean
    public Exchange exchangeDlx(){
        DirectExchange exchange = new DirectExchange("ex.go.dlx",true,false,null);
        return exchange;
    }

    @Bean
    @Resource
    public Binding binding(Queue queue,Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("go").noargs();
    }

    @Bean
    @Resource
    public Binding bindingDlx(Queue queueDlx,Exchange exchangeDlx){
        return BindingBuilder.bind(queueDlx).to(exchangeDlx).with("go.dlx").noargs();
    }
}
